home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / pwdutils.00 / pwdutils / pwdutils-1.00 / pwd_util.c < prev    next >
C/C++ Source or Header  |  1996-05-16  |  5KB  |  229 lines

  1. /*
  2. ** Copyright 1996 Thorsten Kukuk <kukuk@uni-paderborn.de>
  3. **
  4. ** This program is free software; you can redistribute it and/or modify
  5. ** it under the terms of the GNU General Public License as published by
  6. ** the Free Software Foundation; either version 2 of the License, or
  7. ** (at your option) any later version.
  8. **
  9. ** This program is distributed in the hope that it will be useful,
  10. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. ** GNU General Public License for more details.
  13. **
  14. ** You should have received a copy of the GNU General Public License
  15. ** along with this program; if not, write to the Free Software
  16. ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19. #include <pwd.h>
  20. #include <stdio.h>
  21. #include <ctype.h>
  22. #include <malloc.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include <sys/signal.h>
  26. #include <sys/stat.h>
  27. #include <sys/resource.h>
  28.  
  29. #include "pwdutils.h"
  30.  
  31. /*
  32. ** pwd_init - allow unlimited resources and ignore signals.
  33. ** The changing of the passwd shouldn't fail on silly, limited
  34. ** resources
  35. */
  36. void pwd_init()
  37. {
  38.   struct rlimit rlim;
  39.   
  40.   /* 
  41.    * We need unlimited resources and a core file could
  42.    * be a security leak
  43.    */
  44.   rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
  45.   setrlimit(RLIMIT_CPU, &rlim);
  46.   setrlimit(RLIMIT_FSIZE, &rlim);
  47.   setrlimit(RLIMIT_STACK, &rlim);
  48.   setrlimit(RLIMIT_DATA, &rlim);
  49.   setrlimit(RLIMIT_RSS, &rlim);
  50.   rlim.rlim_cur = rlim.rlim_max = 0;
  51.   setrlimit(RLIMIT_CORE, &rlim);
  52.   
  53.   /* 
  54.    * Turn signals off. 
  55.    */
  56.   signal(SIGALRM, SIG_IGN);
  57.   signal(SIGHUP, SIG_IGN);
  58.   signal(SIGINT, SIG_IGN);
  59.   signal(SIGPIPE, SIG_IGN);
  60.   signal(SIGQUIT, SIG_IGN);
  61.   signal(SIGTERM, SIG_IGN);
  62.   signal(SIGTSTP, SIG_IGN);
  63.   signal(SIGTTOU, SIG_IGN);
  64.   
  65.   umask(0);
  66. }
  67.  
  68. /*
  69. ** pwd_error - print error message und remove tmp files befor 
  70. ** exiting the program.
  71. */
  72. void pwd_error(const char *msg,const char *info)
  73. {
  74.   fprintf(stderr, "%s\n%s *NOT* changed. Try again later.\n",msg,info);
  75.   unlink("/etc/passwd.tmp");
  76.  
  77.   exit(1);
  78. }
  79.  
  80. /*
  81. **  prompt - ask the user for a given field and return it,
  82. **  allows the user to use a default value
  83. */
  84. char *prompt (char *question, char *default_value)
  85. {
  86.   int len;
  87.   char *ans;
  88.   static char buf[FILENAME_MAX];
  89.   
  90.   if (! default_value) default_value = "";
  91.   printf("%s [%s]: ", question, default_value);
  92.   *buf = 0;
  93.   if (fgets (buf, sizeof (buf), stdin) == NULL) 
  94.     {
  95.       printf ("\nAborted.\n");
  96.       exit (1);
  97.     }
  98.   /* remove the newline at the end of buf. */
  99.   ans = buf;
  100.   while (isspace (*ans)) ans++;
  101.   len = strlen (ans);
  102.   while (len > 0 && isspace (ans[len-1])) len--;
  103.   if (len <= 0) 
  104.     return strdup(default_value);
  105.   ans[len] = 0;
  106.   return strdup(buf);
  107. }
  108.  
  109. /*
  110. ** set_changed_finger_data - incorporate the new data into the 
  111. ** old passwd entry.
  112. */
  113. int set_changed_finger_data (struct passwd *pwd, struct npwd *newf)
  114. {
  115.   char *gecos;
  116.   int len;
  117.   char changed = 0;
  118.   
  119.   if (newf->full_name)
  120.     changed = 1;
  121.   else
  122.     if(!newf->old_full_name)
  123.       newf->full_name = "";
  124.     else
  125.       newf->full_name = newf->old_full_name;
  126.  
  127.   if (newf->office) 
  128.     changed = 1;
  129.   else
  130.     if(!newf->old_office)
  131.       newf->office = "";
  132.     else
  133.       newf->office = newf->old_office;
  134.   
  135.   if (newf->office_phone) 
  136.     changed = 1;
  137.   else
  138.     if(!newf->old_office_phone)
  139.       newf->office_phone = "";
  140.     else
  141.       newf->office_phone = newf->old_office_phone;
  142.  
  143.   if (newf->home_phone)
  144.     changed = 1;
  145.   else
  146.     if(newf->old_home_phone)
  147.       newf->home_phone = newf->old_home_phone;
  148.     else
  149.       newf->home_phone = "";
  150.  
  151.   if(!newf->other)
  152.     newf->other = "";
  153.  
  154.   len = (strlen (newf->full_name) + strlen (newf->office) +
  155.      strlen (newf->office_phone) + strlen (newf->home_phone) +
  156.      strlen (newf->other) + 4);
  157.   gecos = (char *) xmalloc (len + 1);
  158.   sprintf (gecos, "%s,%s,%s,%s,%s", newf->full_name, newf->office,
  159.        newf->office_phone, newf->home_phone, newf->other);
  160.     
  161.   /* write the new struct passwd to the passwd file. */
  162.   pwd->pw_gecos = gecos;
  163.   
  164.   return changed;
  165. }
  166.  
  167. /*
  168. ** parse_passwd - take a struct password and fill in the fields 
  169. ** of the struct npwd.
  170. */
  171. void parse_passwd (struct passwd *pwd, struct npwd *newf)
  172. {
  173.   char *cptr;
  174.   
  175.   if (pwd) 
  176.     {
  177.        newf->old_username = pwd->pw_name;
  178.       cptr = pwd->pw_gecos;
  179.       newf->old_full_name = cptr;
  180.       cptr = strchr (cptr, ',');
  181.       if (cptr) 
  182.     { 
  183.       *cptr = 0;
  184.       cptr++; 
  185.     } 
  186.       else 
  187.     return;
  188.       newf->old_office = cptr;
  189.       cptr = strchr (cptr, ',');
  190.       if (cptr) 
  191.     { 
  192.       *cptr = 0;
  193.       cptr++; 
  194.     } 
  195.       else 
  196.     return;
  197.       newf->old_office_phone = cptr;
  198.       cptr = strchr (cptr, ',');
  199.       if (cptr) 
  200.     { 
  201.       *cptr = 0; 
  202.       cptr++; 
  203.     } 
  204.       else 
  205.     return;
  206.       newf->old_home_phone = cptr;
  207.       cptr = strchr (cptr, ',');
  208.       if (cptr) { *cptr = 0, cptr++; } else return;
  209.       newf->other = cptr;
  210.     }
  211. }
  212.  
  213. /*
  214. **  xmalloc - malloc, that quit the program with an error message
  215. ** if it fails.
  216. */
  217. void *xmalloc (int bytes)
  218. {
  219.     void *vptr;
  220.  
  221.     vptr = malloc (bytes);
  222.     if (! vptr && bytes > 0)
  223.       {
  224.         perror ("malloc failed");
  225.         exit (1);
  226.       }
  227.     return vptr;
  228. }
  229.